/*
 * @(#)AbstractDataModelResult.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.usf.core.UsfDispatcher;
import com.nhncorp.usf.core.config.AttributeInfo;
import com.nhncorp.usf.core.config.AttributeInfo.ATTRIBUTE_NAME;

/**
 * xml, html, json data model을 위한 Abstract class
 *
 * @author Web Platform Development Team
 */
public abstract class AbstractDataModelResult extends AbstractResult {
	private static final long serialVersionUID = 3087910097392859302L;
	protected static String checkDataModel;
    protected PrintWriter out;
    static {
        checkDataModel = AttributeInfo.getInstance().getAttribute(ATTRIBUTE_NAME.checkDataModel);
    }
    /**
     * generateResultData
     *
     * @param contentType String
     * @param dataMap Map<String, Object> 
     * @return Map<String, Object>
     * @throws IOException Exception
     */
    protected Map<String, Object> generateResultData(String contentType, Map<String, Object> dataMap) throws IOException {
        HttpServletResponse response = ServletHelper.getResponse();
        response.setContentType(contentType + getPageEncoding());
        out = response.getWriter();
        
        Map<String, Object> usfEmbeddedData = new HashMap<String, Object>();
        usfEmbeddedData.put(UsfDispatcher.REQUEST_PARAM_NAME, dataMap.remove(UsfDispatcher.REQUEST_PARAM_NAME));
        usfEmbeddedData.put("context", dataMap.remove("context"));

        if (dataMap.containsKey("usf_querystring")) {
            usfEmbeddedData.put("usf_querystring", dataMap.remove("usf_querystring"));
        }

        if (dataMap.containsKey("usf_uri")) {
            usfEmbeddedData.put("usf_uri", dataMap.remove("usf_uri"));
        }

        dataMap.remove(checkDataModel);
        return usfEmbeddedData;
    }
}
